package me.jiatao.paging3

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadState
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.jiatao.paging3.paging3.FooterAdapter
import me.jiatao.paging3.paging3.ProjectAdapter
import me.jiatao.paging3.paging3.ProjectViewModel
import me.jiatao.paging3.room.ArticleEntity

class MainActivity : AppCompatActivity() {
    private val viewModel by viewModels<ProjectViewModel>()


    @OptIn(ExperimentalPagingApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //初始化控件
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        val progressBar = findViewById<ProgressBar>(R.id.progress_bar)

        //创建列表适配器
        val projectAdapter = ProjectAdapter()
        //添加加载更多
        val footerAdapter = projectAdapter.withLoadStateFooter(FooterAdapter {
            //将重试操作传递到footer
            projectAdapter.retry()
        })

        //初始化列表
        recyclerView.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
            adapter = footerAdapter
        }

        //执行列表数据源绑定
//        lifecycleScope.launchWhenCreated  {
////            withContext(Dispatchers.IO) {
//                viewModel.getProjectPageFlow().collect {
//                    projectAdapter.submitData(it)
//                }
////            }
//        }


        viewModel.getProjectPageFlow().observe(this) {
            projectAdapter.submitData(lifecycle, it)
        }
        

        projectAdapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    progressBar.visibility = View.INVISIBLE
                    recyclerView.visibility = View.VISIBLE
                    println("refresh ---> NotLoading")
                }
                is LoadState.Loading -> {
                    progressBar.visibility = View.VISIBLE
                    recyclerView.visibility = View.INVISIBLE
                    println("refresh ---> Loading")
                }
                is LoadState.Error -> {
                    val state = it.refresh as LoadState.Error
                    progressBar.visibility = View.INVISIBLE
                    Toast.makeText(this, "Load Error: ${state.error.message}", Toast.LENGTH_SHORT)
                        .show()

                    println("refresh ---> Error")
                }
            }

            when (it.append) {
                is LoadState.NotLoading -> {
                    println("append ---> NotLoading")
                }
                is LoadState.Loading -> {
                    println("append ---> Loading")
                }
                is LoadState.Error -> {
                    println("append ---> Error")
                }
            }
        }
    }
}